local assets =
{

}

local prefabs =
{

}

local function GetStringsTable(inst)
    return TUNING.NPC_CHAT_TABLE[ inst.npc_base_lib:Get_Language() ] ["npc_event_clockwork_guard"] or {}
end

-----------------------------------------------------------------------------------------------
--- 灯光相关的控制
-- FRAMES
local function Light_Task_Start(inst)
	if not TheWorld.ismastersim then
		return
	end
	-- TheWorld.state.isday
	-- TheWorld.state.isdusk
	-- TheWorld.state.isnight
	local delta_num = 0.0025
	if TheWorld.state.isnight == true or TheWorld:HasTag("cave") then
		-- print("info:Light_Task_Start  On",inst.__light_up_num)

		----------------------------------------------------------
		-- 关掉旧的任务
		if inst.__light_up_task ~= nil then
			inst.__light_up_task:Cancel()
			inst.__light_up_task = nil
		end
		if inst.__light_off_task ~= nil then
			inst.__light_off_task:Cancel()
			inst.__light_off_task = nil
		end
		-----------------------------------------------------------

		inst.Light:Enable(true)
		if inst.__light_up_num == nil then
			inst.__light_up_num = 0
		end

		inst.__light_up_task = inst:DoPeriodicTask(FRAMES,function()
			if inst.__light_up_num >= 0.55001 then
				inst.__light_up_task:Cancel()
				inst.__light_up_task = nil
				return
			end
			inst.Light:SetIntensity(inst.__light_up_num )
			inst.__light_up_num = inst.__light_up_num + delta_num
		end)
	elseif TheWorld.state.isday then	----------------- light  off
		-- print("info:Light_Task_Start  off")

		if inst.__light_up_task then
			inst.__light_up_task:Cancel()
			inst.__light_up_task = nil
		end

		if inst.__light_up_num == nil or inst.__light_up_num <= 0 then
			inst.Light:Enable(false)
			return
		end

		if inst.__light_up_num > 0 then
			inst.__light_off_task = inst:DoPeriodicTask(FRAMES,function()
				if inst.__light_up_num < 0 then
					inst.Light:Enable(false)
					if inst.__light_off_task then
						inst.__light_off_task:Cancel()
						inst.__light_off_task = nil
					end
					return
				end
				inst.Light:SetIntensity(inst.__light_up_num)
				inst.__light_up_num = inst.__light_up_num  - delta_num
			end)
		end
	else


	end
end
-----------------------------------------------------------------------------------------------
------------- 
----- 背包控制相关
local function container_Widget_change(theContainer)
    local params = {}
    params._theContainerWidget =
    {
        widget =
        {
            slotpos = {},
            animbank = "ui_woby_3x3",
            animbuild = "ui_woby_3x3",
            pos = Vector3(0, 200, 0),
            side_align_tip = 160,
        },
        type = "chest",
        acceptsstacks = true,
        -- usespecificslotsforitems = true,
        -- type = "none",
    }

    for y = 2, 0, -1 do
        for x = 0, 2 do
            table.insert(params._theContainerWidget.widget.slotpos, Vector3(75 * x - 75 * 2 + 75, 75 * y - 75 * 2 + 75, 0))
        end
    end
    function params._theContainerWidget.itemtestfn(container, item, slot)
        if item.prefab == "goldnugget" then
            return true
        end
        return false
    end


    theContainer:WidgetSetup("wobysmall",params._theContainerWidget)
	-- if TheWorld.ismastersim then
    --     theContainer:SetNumSlots(#params._theContainerWidget.widget.slotpos)
    -- end
end

local function add_container_before_not_ismastersim_retrun(inst)
	-------------------------------------------------------------------------------------------------
    ------ 添加背包container组件
    --    local container_WidgetSetup = "wobysmall"
    if TheWorld.ismastersim then
        inst:AddComponent("container")
        inst.components.container.openlimit = 1  ---- 限制1个人打开
    --    inst.components.container:WidgetSetup(container_WidgetSetup)
        container_Widget_change(inst.components.container)
		-- inst.components.container.numslots = 9 -- 格子数 -- 不能用这个，会造成bug
    else
        inst:DoTaskInTime(0.1, function()
            if inst.replica then
                if inst.replica.container then
                --    inst.replica.container:WidgetSetup(container_WidgetSetup)
                container_Widget_change(inst.replica.container)
                end
            end

        end)
    end
    -------------------------------------------------------------------------------------------------
end
-----------------------------------------------------------------------------------------------


local function add_talker_befor_not_ismastersim_retrun(inst)
	inst:AddComponent("talker")
    inst.components.talker.fontsize = 30
    inst.components.talker.font = TALKINGFONT
    inst.components.talker.colour = Vector3(255/255, 255/255, 0/255)
    inst.components.talker.offset = Vector3(0, -950, 0)
    -- inst.components.talker:MakeChatter()
end


-----------------------------------------------------------------------------------------------
local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddMiniMapEntity()
	inst.entity:AddNetwork()
	inst.entity:AddLight()
	
	-----------------------------------------------------------------------------------------------
	-- inst.MiniMapEntity:SetPriority(5)
    inst.MiniMapEntity:SetIcon("carnivalgame_herding_station.png")


	MakeObstaclePhysics(inst, .5)

    inst.Light:Enable(false)
    inst.Light:SetRadius(5)
    -- inst.Light:SetIntensity(0.55)
    inst.Light:SetIntensity(0)
    inst.Light:SetFalloff(1.3)
    inst.Light:SetColour(251/255, 240/255, 218/255)
	inst:DoTaskInTime(0.5,function()
		Light_Task_Start(inst)
	end)



    inst.AnimState:SetBank("carnivalgame_herding_station")
    inst.AnimState:SetBuild("carnivalgame_herding_station")
    inst.AnimState:PlayAnimation("idle_off", true)
	-- inst.AnimState:PlayAnimation("turn_on", false)
	-- inst.AnimState:PushAnimation("on", true)


    inst:AddTag("structure")

	MakeSnowCoveredPristine(inst)


    inst.entity:SetPristine()

	------ 添加背包
	add_container_before_not_ismastersim_retrun(inst)
	add_talker_befor_not_ismastersim_retrun(inst)

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("npc_base_lib")
    inst:AddComponent("npc_everything_data")



    inst:AddComponent("inspectable")
    inst:AddComponent("named")
    inst.components.named:SetName(GetStringsTable(inst).tree_name)
    STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper("npc_item_clockwork_guard_station")] = GetStringsTable(inst).inspect_str  --人物检查的描述

    -- inst:AddComponent("savedrotation")
	inst:AddComponent("lootdropper")
    inst.components.lootdropper:SetLoot({"npc_item_clockwork_guard_station_kit"})	--- 被锤后得回原来的东西，但是数据清空

	inst:AddComponent("workable")
	inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
	inst.components.workable:SetWorkLeft(3)
    inst.components.workable:SetOnFinishCallback(function()
		inst.components.lootdropper:DropLoot()
		-- 回收金块
		inst:PushEvent("Return_Back_Gold")
		---
		inst:Remove()
	end)
	inst.components.workable:SetOnWorkCallback(function()
		inst.AnimState:PlayAnimation("hit")
		if inst.__turn_on == true then
			inst.AnimState:PushAnimation("on", true)
		else
			inst.AnimState:PushAnimation("idle_off", true)
		end
	end)

    MakeSnowCovered(inst)



	-- -----------------------------------------------------------------------------------
	inst:WatchWorldState("isnight",function()	-------- 晚上
		if TheWorld.state.isnight then
			inst:DoTaskInTime(3,function()
				Light_Task_Start(inst)			
			end)
		end
	end)
	-- -- -----------------------------------------------------------------------------------

	require("prefabs/items/05_clockwork_guard_events")():Init(inst)
	-----------------------------------------------------------------------------------
	---------- 开关状态切换
	inst:ListenForEvent("turn_on",function()
		if inst.__turn_on == true then
			return
		end
		inst.__turn_on = true
		inst.AnimState:PlayAnimation("turn_on", false)
		inst.AnimState:PushAnimation("on", true)
	end)

	inst:ListenForEvent("turn_off",function()
		if inst.__turn_on ~= true then
			return
		end
		inst.__turn_on = false
		inst.AnimState:PlayAnimation("idle_off", true)
	end)
	-----------------------------------------------------------------------------------

    return inst
end




----------------------------------------------------------------------------------------------------------------------------------------------------------------------
local deployable_data = ------ 放置物品的inst的创建,MakeDeployableKitItem 调用的 fn
{
	deploymode = DEPLOYMODE.CUSTOM,
	custom_candeploy_fn = function(inst, pt, mouseover, deployer)   --- 放置时候的检查fn
        -- function Map:IsDeployPointClear(pt, inst, min_spacing, min_spacing_sq_fn, near_other_fn, check_player, custom_ignore_tags)
        local min_spacing = 2   --- 半径
        local ignore_tag = {"player","INLIMBO","FX","animal","smallcreature","flight","character","smallcreature","insect","companion","invisible","chester","hutch"}
        return TheWorld.Map:IsDeployPointClear(pt,inst,min_spacing,nil,nil,nil,ignore_tag)


        -- return false	
    end,

    master_postinit = function(inst)

        inst:AddComponent("npc_base_lib")
        inst:AddComponent("npc_everything_data")


        inst.components.inventoryitem:ChangeImageName("carnivalgame_herding_kit") --- 设置物品在背包里的贴图
        
        
        inst:AddComponent("named")
        inst.components.named:SetName(GetStringsTable(inst).tree_name)
        STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper("npc_item_clockwork_guard_station_kit")] = GetStringsTable(inst).kit_inspect_str  --人物检查的描述




    end,
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

local function placer_postinit_fn(inst)
	-- UpdateArtForRank(inst, 1, 1, true)	--- 放置的时候只显示阶段1
    -- CreateFloor(inst)
	-- -- inst:AddComponent("npc_base_lib")
	-- -- inst.npc_base_lib = inst.components.npc_base_lib
	-- -- local chat_table = GetStringsTable(inst)
	-- -- inst.npc_base_lib:Announce(chat_table.announce)
end

return Prefab("npc_item_clockwork_guard_station", fn, assets, prefabs),
		MakeDeployableKitItem("npc_item_clockwork_guard_station_kit", "npc_item_clockwork_guard_station", "carnivalgame_herding_station", "carnivalgame_herding_station", "kit_item", assets, {size = "med", scale = 0.77}, nil, {fuelvalue = TUNING.LARGE_FUEL}, deployable_data),
		MakePlacer("npc_item_clockwork_guard_station_kit_placer", "carnivalgame_herding_station", "carnivalgame_herding_station", "idle_off", nil, nil, nil, nil, 90, nil, placer_postinit_fn)
